<?php


namespace App\Http\Controllers\Admin\Critical;


use App\Enums\RedisKeyEnum;
use App\Exceptions\BasicException;
use App\Http\Controllers\BaseController;
use App\Response\Response;
use App\Traits\AdminLog;
use Illuminate\Http\Request;
//暴击奖池幸运树规则
class CriticalTreeRuleController extends BaseController
{
    use AdminLog;
    /**
     * @api {put} /critical/rule 暴击规则新增/编辑
     *
     * @apiGroup  暴击奖池
     *
     * @apiParam {int} open_level 开启等级
     * @apiParam {Int} max_hit_times 最大暴击倍率
     *
     * @apiSuccessExample {json} Response-Example
     * {"code":200,"data":[],"msg":"设置成功"}
     * {
    "code": 0,
    "message": "设置的暴击次数需比高等级暴击次数小"
    }
     */
    public function set(Request $request)
    {
        $param = $request->only('open_level', 'max_hit_times');
        $v = app('validator')->make($param, [
            'open_level' => 'required|integer|min:0',
            'max_hit_times' => 'required|integer|min:2|max:10',
        ]);
        if ($v->fails()){
            throw new BasicException(0, $v->errors()->first());
        }
        $ruleList = app('model_critical_tree_rule')->where([
            'is_del' => 0
        ])->orderBy('open_level','desc')->get();
        $ruleList = empty($ruleList)?[]:$ruleList->toArray();
        $isAdd = true;
        $data = [
            'open_level' => $param['open_level'],
            'max_hit_times' => $param['max_hit_times'],
            'is_del' => 0,
        ];
        $maxMaxHitTimes = 0;    //比他大的
        $minMaxHitTimes = 0;    //比他小的

        foreach ($ruleList as $v){
            if($v['open_level'] > $param['open_level']){
                $maxMaxHitTimes = $v['max_hit_times'];
            }else if($v['open_level'] == $param['open_level']){
                $isAdd = false;
            }else{
                if(empty($minMaxHitTimes)){
                    $minMaxHitTimes = $v['max_hit_times'];
                }
            }
        }
        if(!empty($maxMaxHitTimes) && $maxMaxHitTimes<$param['max_hit_times']){
            throw new BasicException(0, '设置的暴击次数需比高等级暴击次数小');
        }
        if(!empty($minMaxHitTimes) && $minMaxHitTimes>$param['max_hit_times']){
            throw new BasicException(0, '设置的暴击次数需比低等级暴击次数大');
        }
        if($isAdd){
            $res = app('model_critical_tree_rule')->insert($data);
        }else{
            $res = app('model_critical_tree_rule')->where(['open_level'=>$param['open_level']])->update([
                'max_hit_times' => $param['max_hit_times']
            ]);
        }
        if(!$res){
            throw new BasicException(0, '设置失败');
        }
        $key = RedisKeyEnum::CRITICAL_TREE_RULE;
        app('redis')->del($key);
        return Response::sendData([],'设置成功');
    }


    /**
     * @api {delete} /critical/rule 删除暴击规则配置
     *
     * @apiGroup  暴击奖池
     *
     * @apiParam {int} open_level 开启等级
     *
     * @apiSuccessExample {json} Response-Example
     * {"code":200,"data":[],"msg":"删除成功"}
     */
    public function del(Request $request)
    {
        $param = $request->only('open_level', 'max_hit_times');
        $v = app('validator')->make($param, [
            'open_level' => 'required|integer|min:0'
        ]);
        if ($v->fails()){
            throw new BasicException(0, $v->errors()->first());
        }
        $rule = app('model_critical_tree_rule')->where([
            'is_del' => 0,
            'open_level' => $param['open_level'],
        ])->orderBy('open_level','desc')->first();
        $rule = empty($rule)?[]:$rule->toArray();
        if(empty($rule)){
            return Response::sendData([],'删除成功');
        }
        $res = app('model_critical_tree_rule')->where(['open_level'=>$param['open_level']])->update([
            'is_del' => 1
        ]);
        if(!$res){
            throw new BasicException(0, '删除失败');
        }
        $key = RedisKeyEnum::CRITICAL_TREE_RULE;
        app('redis')->del($key);
        return Response::sendData([],'删除成功');
    }

    /**
     * @api {get} /critical/rule 暴击规则列表
     * @apiGroup  暴击奖池
     *
     * @apiSuccessExample {json} Response-Example
     * {
    "code": 200,
    "data": [
    {
    "id": 2,
    "open_level 开启等级": 3,
    "max_hit_times 最大暴击倍数": 2,
    "is_del": 0,
    "created_at 创建时间": "2021-10-20 10:15:12",
    "updated_at": "2021-10-20 15:07:47",
    "people_num 满足条件人数": 1
    },
    {
    "id": 1,
    "open_level": 1,
    "max_hit_times": 2,
    "is_del": 0,
    "created_at": "2021-10-20 10:15:08",
    "updated_at": "2021-10-20 10:15:08",
    "people_num": 1
    }
    ],
    "message": ""
    }
     */
    public function all(Request $request)
    {
        $rule = app('model_critical_tree_rule')->where([
            'is_del' => 0,
        ])->orderBy('open_level','asc')->get();
        $rule = empty($rule)?[]:$rule->toArray();
        if(empty($rule)){
            return Response::sendData([]);
        }
        $countRule = count($rule)-1;
        foreach ($rule as $k=>$v){
            if($k <$countRule){
                $count = app('model_user')->where('rank_id', '>=',$v['open_level'])->where('rank_id', '<', $rule[$k+1]['open_level'])->count();
            }else{
                $count = app('model_user')->where('rank_id', '>=', $v['open_level'])->count();
            }
            $rule[$k]['people_num'] = $count;
        }
        return Response::sendData($rule);
    }
}